package com.hanyxx.io;

import java.io.*;
import java.util.Collection;
import java.util.TreeMap;

/**
 * 文件排序
 * @author layman
 * @date 2021/3/10
 */
public class Demo09 {
    public static void main(String[] args) throws IOException {
        BufferedReader fos = new BufferedReader(new FileReader("出师表.txt"));
        BufferedWriter fis = new BufferedWriter(new FileWriter("出师表_copy.txt"));
        TreeMap<String,String> treeMap = new TreeMap<>();

        String read;
        while((read = fos.readLine()) != null){
            // TreeMap使用自然排序
            treeMap.put(read.substring(0,1),read);
        }

        Collection<String> values = treeMap.values();
        for (String value : values) {
            // 遍历集合，换行写入
            //fis.write(value);
            // 写入时去除开头的行号
            fis.write(value.substring(2));
            fis.newLine();
        }

        // 关闭流
        fis.close();
        fos.close();
    }
}
